﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Cotizav.CONEXION;
using System.Transactions;
using System.Windows.Forms;

namespace Cotizav
{
    public class CtrlFormato
    {
        ConexionDataContext conexion;

        internal List<FORMATO> getListFormatos()
        {
            using (conexion = new ConexionDataContext())
            {
                try
                {
                    List<FORMATO> result = conexion.FORMATO.Where(x => x.eliminado == 0).ToList();
                    return result;
                }
                catch (Exception)
                {                    
                    throw;
                }
            }
        }

        internal System.Windows.Forms.DialogResult registrarFormato(FORMATO format)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                using (conexion = new ConexionDataContext())
                {
                    try
                    {
                        DialogResult result = DialogResult.Cancel;

                        int correlativoformato = conexion.COTIZAV_CORRELATIVO.Single(x => x.IDENTIFICADOR == "COTIZAV").FORMATO.Value;
                        string num = Convert.ToString(correlativoformato);
                        string numero = num.Trim().PadLeft(11, '0');
                        string idFormato = "FOR_" + numero;

                        format.idFormato = idFormato;

                        conexion.FORMATO.InsertOnSubmit(format);
                        conexion.SubmitChanges();
                        conexion.increm_ContFormato();

                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal System.Windows.Forms.DialogResult actualizarFormato(FORMATO format)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                using (conexion = new ConexionDataContext())
                {
                    try
                    {
                        FORMATO formato = conexion.FORMATO.Single(x => x.idFormato == format.idFormato);
                        DialogResult result = DialogResult.Cancel;

                        formato.nombreEncabezado = format.nombreEncabezado;
                        formato.eliminado = format.eliminado;
                        formato.esDefecto = format.esDefecto;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                        result = DialogResult.OK;

                        return result;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }
        }

        internal void eliminarFormato(FORMATO format)
        {
            using (TransactionScope transactionScope = new TransactionScope())
            {
                using (conexion = new ConexionDataContext())
                {
                    try
                    {
                        FORMATO form = conexion.FORMATO.Single(x => x.idFormato == format.idFormato);
                        form.eliminado = 1;

                        conexion.SubmitChanges();
                        transactionScope.Complete();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
            }

        }
    }
}
